package pl.wr.physics.units.quantum.number;

import pl.wr.physics.exceptions.QuantumException;
import pl.wr.physics.units.AbstractUnit;
import pl.wr.physics.units.SI.Prefix;
import pl.wr.physics.units.quantum.QuantumUnit;

/**
 * @author wieslaw.rodak
 * 
 */
public class AzimuthalQuantumNumber implements QuantumUnit {

    /**
     * Azimuthal quantum number.
     */
    private int l;

    /**
     * Make azimuthal quantum number from range: 0-6. Oversize throw
     * QuantumException.
     * 
     * @param l
     *            - int value of azimuthal quantum number.
     */
    public AzimuthalQuantumNumber(int l) {
        checkScope(l);
        this.l = l;
    }

    /**
     * @return int value of azimuthal quantum number
     */
    public int getL() {
        return l;
    }

    /**
     * Set the azimuthal quantum number from range: 0-6. Oversize throw
     * QuantumException.
     * 
     * @param l
     *            - int value of azimuthal quantum number.
     */
    public void setL(int l) {
        checkScope(l);
        this.l = l;
    }

    private static class AzimuthalQuantumNumberCache {
        static final AzimuthalQuantumNumber cache[] = new AzimuthalQuantumNumber[7];

        static {
            for (int k = 0; k < cache.length; k++)
                cache[k] = new AzimuthalQuantumNumber(k);
        }

        private AzimuthalQuantumNumberCache() {
        }
    }

    /**
     * Returns an AzimuthalQuantumNumber instance representing the specified int
     * value. This method will always cache values in the range 0 to 6.
     * 
     * @param l
     * @return
     */
    public static AzimuthalQuantumNumber valueOf(int l) {
        checkScope(l);
        return AzimuthalQuantumNumberCache.cache[l];
    }

    private static void checkScope(int l) {
        if (l >= 0 && l <= 6) {
            // is OK
        } else {
            throw new QuantumException("Bad scope of number: " + l);
        }
    }

    @Override
    public AbstractUnit getSIValue() {
        return new AbstractUnit(Prefix.basic, l, null) {
        };
    }
}
